//
//  YHUserAccountTools.swift
//  YHWeibo
//
//  Created by yuhao.mac on 16/6/26.
//  Copyright © 2016年 yuhao.mac. All rights reserved.
//

import UIKit

class YHUserAccountTools: NSObject {
    
    //路径
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    //保存的用户信息数据对象
    var userAccount: YHUserAccount?
    
    //判断用户是否登录
    var isLogin:Bool{
        return accessToken != nil
    }
    
    //访问令牌
    var accessToken: String?{
        if userAccount?.access_token == nil{
            return nil
        }else{
            //如果当前时间没有过期
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userAccount?.access_token
            }else{
                return nil
            }
        }
    }
    
    //全局访问接口
    static let sharedTools:YHUserAccountTools = YHUserAccountTools()
    
    override init() {
        super.init()
        
        userAccount = getUserAccount()
    }
    
    //保存对象
    func saveUserAccount(userAccount:YHUserAccount){
        
        self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
    }
    //获取对象
    func getUserAccount() ->YHUserAccount?{
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? YHUserAccount
        return result
    }
}











